#include "framewindow.h"
#include "serialportsmanager.h"


#include <QMainWindow>
#include <QMessageBox>

#include "ui_mainwindow.h"
#include "QDebug"

SerialPortsManager::SerialPortsManager( QMainWindow *win)
{
    mainwidow = win;
    serialPort = new QSerialPort();
   // serialPort = COM;
}

int SerialPortsManager::loadConfigs()
{
    baudRateInt = 9600;
    dataBits= QSerialPort::Data8;
    stopBits= QSerialPort::OneStop;
    parity= QSerialPort::NoParity;
    flowControl= QSerialPort::NoFlowControl;

    return 0;

}

int SerialPortsManager::configCOM()
{
    // 设置波特率
    //com1->setBaudRate(9600);
    //baudRate = QSerialPort::Baud9600;
    baudRateInt = 9600;
    //设置数据位数
    switch(8)//ui->dataBox->currentIndex()
    {
    case 5:
        dataBits = QSerialPort::Data5;break;
    case 6: dataBits = QSerialPort::Data6;break;
    case 7: dataBits = QSerialPort::Data7; break;
    case 8: dataBits = QSerialPort::Data8; break;
    default: break;
    }
    // 设置校验位
    //SerialPort->setParity(QSerialPort::NoParity);
    //设置奇偶校验
    switch(2)//ui->checkBox->currentIndex()
    {
    case 0: parity = QSerialPort::NoParity; break;
    case 1: parity = QSerialPort::OddParity; break;
    case 2: parity = QSerialPort::EvenParity; break;
    default: break;
    }
    // 设置流控制
    // com1->setFlowControl(QSerialPort::NoFlowControl);
    switch(0)//ui->stopBox->currentIndex()
    {
    case 0: flowControl  = QSerialPort::NoFlowControl; break;
    case 1: flowControl =  QSerialPort::HardwareControl; break;
    case 2: flowControl  = QSerialPort::SoftwareControl; break;
    default:
        flowControl = QSerialPort::UnknownFlowControl;break;
    }
    //设置停止位
    switch(1)//ui->stopBox->currentIndex()
    {
    case 1: stopBits = QSerialPort::OneStop; break;
    case 2: stopBits = QSerialPort::TwoStop; break;
    default: break;
    }

    return 0;
}

int SerialPortsManager::openCOM(QString portName)
{

    // 设置串口号
    //QSerialPort *serialPort = serialPorts[0];
    serialPort->setPortName(portName);
    if(serialPort->open(QIODevice::ReadWrite))
    {
        serialPort->setBaudRate(baudRateInt);
        serialPort->setDataBits(dataBits);
        serialPort->setParity(parity);
        serialPort->setStopBits(stopBits);
        serialPort->setFlowControl(flowControl);

        //QMessageBox::about(nullptr, "提示", portName+"串口打开成功");

        //连接信号槽 当下位机发送数据QSerialPortInfo 会发送个 readyRead 信号,我们定义个槽void receiveInfo()解析数据
        // QObject::connect(serialPort, &QSerialPort::readyRead
        //                 ,this, &SerialPortsManager::receiveCOM); //Qt::QueuedConnection

        return 0;
    }
    //打开串口
    else
    {
        QMessageBox::about(nullptr, "提示", "串口无法打开\r\n不存在或已被占用");
        return 1;
    }

}

int SerialPortsManager::closeCOM()
{

    serialPort->close();
    //QMessageBox::about(nullptr, "提示",  "串口关闭了");
    return 0;
}


int SerialPortsManager::sendCOM( QSerialPort *com,const char * data)
{

    com->write(data);
    int byteLen = (int)(com->bytesToWrite());                        //输出写数据的字节数
    return byteLen;
}

int SerialPortsManager::sendCOM(const char * data)
{
    int byteLen =0;
    if(serialPort!=nullptr){
        serialPort->write(data);
        serialPort->waitForBytesWritten(500);// 等待写入成功,must
        byteLen = (int)(serialPort->bytesToWrite());                        //输出写数据的字节数
    }


    return byteLen;

}

bool SerialPortsManager::waitCondition( unsigned long time)
{
    return waitCon.wait(&mutex,time);
}

bool SerialPortsManager::waitForReceive(int time)
{
    // qDebug() << "串口名称： "<<COMName;
    return serialPort->waitForReadyRead(time);
}

/*
void SerialPortsManager::receiveCOM()
{
    //从缓冲区中读取数据
    QByteArray buffer = serialPort->readAll();
    qDebug() << "received "<<buffer;

    if(!buffer.isEmpty())//如果非空说明有数据接收
    {
        mutex.lock();
        newReceived = buffer;
        waitCon.wakeAll();

        mutex.unlock();

        ((FrameWindow*)mainwidow)->getUI()->textEditReceive->append(newReceived+"\r\n");

    }

    //清除缓冲区
    buffer.clear();
}
*/





